#include "login.h"
#include "../auth/auth.h"
#include "../utils/log.h"
#include "lv_drv_conf.h"
#include <stdio.h>
#include "../utils/msg.h"

// 标题
static lv_obj_t *title_label;

// 登录按钮
static lv_obj_t *login_btn;

// 注册按钮
static lv_obj_t *register_btn;

// 账号文本框
static lv_obj_t *account_txt_area;

// 密码文本框
static lv_obj_t *password_txt_area;

// 欢迎页面
static lv_obj_t *login_page;

static lv_obj_t *keyboard;

void login_btn_event_cb(lv_event_t *e) {
    char *account = lv_textarea_get_text(account_txt_area);
    char *password = lv_textarea_get_text(password_txt_area);
    log_info("account: %s, password: %s", account, password);
    response_t *resp = auth_login(account, password);
    if (resp->code == RESPONSE_OK) {
        login_page_destroy();
        game_room_list_page_display();
    } else {
        log_info("%s", resp->message);
    }
    response_free(resp);
}

void login_back_welcome(lv_event_t *e) {
    login_page_destroy();
    welcome_page_display();
}

static void ta_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);
    if (code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(kb, ta);
        // 页面组件整体向上偏移
        lv_obj_set_y(login_page, -lv_obj_get_y(ta) + 50);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    } else if (code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_set_y(login_page, 0);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
}

// 欢迎页面初始化
void login_page_display() {
    msg_init();
    // 创建欢迎页面
    login_page = lv_obj_create(lv_scr_act());
    // 设置界面大小
    lv_obj_set_size(login_page, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_radius(login_page, 0, 0);
    lv_obj_set_style_border_width(login_page, 0, 0);
    lv_obj_set_style_outline_width(login_page, 0, 0);

    // 创建背景对象
    static lv_style_t style_bg;
    lv_style_init(&style_bg);
    lv_style_set_bg_img_src(&style_bg, &login_and_register);
    lv_obj_add_style(login_page, &style_bg, LV_PART_MAIN);

    // 添加界面元素
    title_label = lv_label_create(login_page);
    lv_obj_set_size(title_label, 290, 50);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 50, 50);
    lv_obj_set_style_text_font(title_label, &lv_font_montserrat_40, 0);
    lv_label_set_text(title_label, "login");

    account_txt_area = lv_textarea_create(login_page);
    lv_obj_set_size(account_txt_area, 250, 50);
    lv_obj_set_style_text_font(account_txt_area, &lv_font_montserrat_20, 0);
    lv_obj_align(account_txt_area, LV_ALIGN_TOP_MID, 0, 120);

    password_txt_area = lv_textarea_create(login_page);
    lv_obj_set_size(password_txt_area, 250, 50);
    lv_obj_set_style_text_font(password_txt_area, &lv_font_montserrat_20, 0);
    lv_textarea_set_password_mode(password_txt_area, true);
    lv_obj_align_to(password_txt_area, account_txt_area,
                    LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

    // 创建按钮区
    lv_obj_t *btn_area = lv_obj_create(login_page);
    lv_obj_set_size(btn_area, 290, 50);
    lv_obj_align_to(btn_area, password_txt_area, LV_ALIGN_OUT_BOTTOM_MID, 0,
                    20);
    lv_obj_add_flag(btn_area, LV_OBJ_FLAG_HIDDEN);

    // 初始化login按钮

    login_btn = lv_btn_create(login_page);
    lv_obj_set_size(login_btn, 100, 50);
    lv_obj_align_to(login_btn, btn_area, LV_ALIGN_LEFT_MID, 0, 0);
    // 设置事件，传递输入框数据
    lv_obj_add_event_cb(login_btn, login_btn_event_cb, LV_EVENT_CLICKED, NULL);

    // 设置按钮标签
    lv_obj_t *login_label = lv_label_create(login_btn);
    lv_obj_align_to(login_label, login_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(login_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(login_label, "Login");

    // 初始化register按钮
    register_btn = lv_btn_create(login_page);
    lv_obj_set_size(register_btn, 100, 50);
    lv_obj_align_to(register_btn, btn_area, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_add_event_cb(register_btn, login_back_welcome, LV_EVENT_CLICKED,
                        NULL);

    // 设置按钮标签
    lv_obj_t *register_label = lv_label_create(register_btn);
    lv_obj_align_to(register_label, register_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(register_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(register_label, "Back");

    // 初始化键盘
    // 键盘
    keyboard = lv_keyboard_create(lv_scr_act());
    lv_obj_set_style_text_font(keyboard, &lv_font_dejavu_16_persian_hebrew, 0);

    lv_obj_add_event_cb(account_txt_area, ta_event_cb, LV_EVENT_ALL, keyboard);
    lv_obj_add_event_cb(password_txt_area, ta_event_cb, LV_EVENT_ALL, keyboard);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}

// 欢迎页面销毁
void login_page_destroy() {
    
    // 销毁界面元素
    lv_obj_del(login_page);
    // keyboard 父类为初始屏幕对象，手动删除
    lv_obj_del(keyboard);
}